//
//  MantuoluoListViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2018/3/5.
//  Copyright © 2018年 史宵宵. All rights reserved.
//

#import "MantuoluoListViewController.h"
#import "GLSegmentedControl.h"
#import "PictureListViewController.h"
#import "ImaginationQuietViewController.h"

@interface MantuoluoListViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,assign) NSInteger selectIndex;
@end

@implementation MantuoluoListViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"曼陀罗";
    UIImage *image = [UIImage imageNamed:@"mantuoluoIntroduce_bg"];
    self.view.layer.contents = (id) image.CGImage;
    
    self.selectIndex = 1;
    
    [self creatHeadView];
    [self creatData];
    
    
}
- (void)creatHeadView{
    GLSegmentedControl *choiceview=[[GLSegmentedControl alloc]initWithFrame:CGRectMake((kScreenW - 200)/2, 40, 200, 50) andTitleArray:@[@"涂鸦篇",@"畅想篇"]];
    __weak typeof(self)weakSelf = self;
    choiceview.selectindex = ^(NSInteger index) {
        weakSelf.selectIndex = index + 1;
        
        [weakSelf creatData];
//        [weakSelf.tableView reloadData];
    };
    [self.view addSubview:choiceview];
}
- (void)creatData{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [dic setValue:[NSString stringWithFormat:@"%ld",self.selectIndex] forKey:@"type"]; //1:涂鸦 2 畅想
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppInterestGetGuanQiaList] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [self.dataArray removeAllObjects];
            [self.dataArray addObjectsFromArray: [NSMutableArray arrayWithArray:response]];
            [self removeallSubViews:self.view];
            [self creatMenu];
            
        }
    }];
}
- (void )removeallSubViews:(UIView *)supView{
    for (id obj in supView.subviews) {
        if ( [obj isKindOfClass:[UIButton class]]) {
            [obj removeFromSuperview];
        }
    }
}
- (void)creatMenu{
    //    NSInteger menuLengt = 12;
    NSInteger num = 4;
    double width = 60;
    double margin = (kScreenW - num*60)/(num + 1);
    for (int i = 0; i <self.dataArray.count; i ++) {
        UIButton *menuButton = [[UIButton alloc]init];
        menuButton.frame = CGRectMake(margin + (margin + width) * (i % num), 120 + (margin + width) * (i / num), width, width);
        menuButton.tag = 1000 + i;
        [menuButton setTitleColor:KTEXTMAINCOLOR forState:UIControlStateNormal];
        if ([self.dataArray[i][@"pass_history"] intValue] == 1) {  //1 ：已解锁 0 未解锁
            [menuButton setBackgroundImage:[UIImage imageNamed:@"ins_watch_menu_select"] forState:UIControlStateNormal];
            [menuButton setTitle:[NSString stringWithFormat:@"%d",i+ 1] forState:UIControlStateNormal];
        }else{
            [menuButton setBackgroundImage:[UIImage imageNamed:@"ins_watch_menu_unselect"] forState:UIControlStateNormal];
        }
        menuButton.tag = 1000 + i;
        [menuButton addTarget:self action:@selector(addPassLog:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:menuButton];
    }
    
}
- (void)addPassLog:(UIButton *)sender{
    NSDictionary *dic = self.dataArray[sender.tag - 1000];
    if ([dic[@"pass_history"] intValue] == 1) {  //1 ：已解锁进详情页 0 未解锁，调解锁接口
        if (self.selectIndex == 1) {
            PictureListViewController *pictureListVC = [[PictureListViewController alloc]init];
            pictureListVC.pictureListId = dic[@"id"];
            [self.navigationController pushViewController:pictureListVC animated:YES];
        }else{
            ImaginationQuietViewController *pictureListVC = [[ImaginationQuietViewController alloc]init];
            pictureListVC.pictureListId = dic[@"id"];
            [self.navigationController pushViewController:pictureListVC animated:YES];
        }
    }else{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message: [NSString stringWithFormat:@"解锁此关卡需要消耗%@积分",dic[@"score"]] preferredStyle:UIAlertControllerStyleAlert];
        
        // Create the actions.
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
        }];
        
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
            [dic setValue:self.dataArray[sender.tag - 1000][@"id"] forKey:@"id"]; //关卡id
            [yjNetWorkManger requestType:GET andURL:[JSPathUtil urlAppInterestGetGuanQiaById] andParmas:dic andComplition:^(id response, BOOL isuccess) {
                if (isuccess) {
                    [sender setBackgroundImage:[UIImage imageNamed:@"ins_watch_menu_select"] forState:UIControlStateNormal];//self.dataArray[sender.tag - 1000][@"name"]
                    [sender setTitle:[NSString stringWithFormat:@"%ld",sender.tag - 1000 + 1] forState:UIControlStateNormal];
                    //解锁成功进详情页
                    if (self.selectIndex == 1) {
                        PictureListViewController *pictureListVC = [[PictureListViewController alloc]init];
                        pictureListVC.pictureListId = dic[@"id"];
                        [self.navigationController pushViewController:pictureListVC animated:YES];
                    }else{
                        ImaginationQuietViewController *pictureListVC = [[ImaginationQuietViewController alloc]init];
                        pictureListVC.pictureListId = dic[@"id"];
                        [self.navigationController pushViewController:pictureListVC animated:YES];
                    }
                }
            }];
        }];
        
        // Add the actions.
        [alertController addAction:cancelAction];
        [alertController addAction:otherAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}
- (void)enterWatch:(UIButton *)sender{
//    WatchProblemViewController *watchProblemVC = [[WatchProblemViewController alloc]init];
//    [self.navigationController pushViewController:watchProblemVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
